home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 9.9 KB | 348 lines | [TEXT/ttxt] |
- --<<<-
- -- Filename:
- -- mediaimp.sx
-
- -- Other Files Required:
- -- This file is not part of any specific example, but rather a useful class for
- -- importing media and storing the media into ObjectStore.
-
- -- Purpose:
- -- Provide a layer to the import export engine which enables easy use of ObjectStore.
-
- -- Specialized Classes:
- -- MediaImporter
-
- -- Instructions to User:
- -- Class MediaImporter provides a layer to the import export engine
- -- which enables easy use of ObjectStore. When you create a MediaImporter,
- -- you may specify a storage container in which media is automatically stored,
- -- and a target collection to which media is automatically added. The target
- -- must be an ExplicitKeyedCollection subclass (e.g. HashTable). When you are
- -- done importing, you can invoke the MediaImporter saveMedia method to register
- -- the hash table with the storage container, and then store the hash table.
- -- For each import method, you specify a fileName and optional mediaKey. The
- -- import adds an entry into the ExplicitKeyedCollection using the filename as
- -- the key, unless the mediaKey is supplied. In this case, mediaKey is used as
- -- the key.
-
- -- PUBLIC PROTOCOL
- -- importObject self fileName key:mediaKey
- -- importDIB self fileName key:mediaKey
- -- importPICT self fileName key:mediaKey
- -- importRTF self fileName key:mediaKey
- -- importAIFF self fileName key:mediaKey
- -- importWave self fileName key:mediaKey
- -- importQuicktime self fileName key:mediaKey
- -- importRes self type id key:mediaKey
- -- saveMedia self name
-
- -- KEYWORD ARGUMENTS
- -- path -- path from which to import media.
- -- container -- storage container for media
- -- target -- target ExplicitKeyedCollection
- -- addExtensions -- if true, file extensions will be added for specific media.
- -- extension -- extension to add
- -- mediaCategory -- @image, @sound, @movie
- -- inputMediaType -- @dib, @pict, etc.
- -- outputMediaType -- @bitmap, @player, etc.
-
- -- EXAMPLE CREATION
- -- An importer which imports from the StartDir:
- -- mi := new MediaImporter
-
- -- An importer which imports from a folder called "media", and stores media in
- -- storage container c/hashtable h:
- -- mi := new MediaImporter path:(spawn theStartDir "media") container:c target:h
-
- -- Author:
- -- Steve Mayer
-
- in module InternetFish
-
-
- class MediaImporter ()
- class variables
- extensionMapping -- mapping of file extensions to import parameters.
- instance variables
- path -- path from which to import media.
- container -- storage container for media
- target -- target ExplicitKeyedCollection
- addExtensions -- if true, file extensions will be added for specific media.
- smartExtensions -- if true, file type is inferred from extension.
- extension -- extension to add
- mediaCategory -- @image, @sound, @movie
- inputMediaType -- @dib, @pict, etc.
- outputMediaType -- @bitmap, @player, etc.
- bundle -- ResBundle instance, if we are importing Res files.
- convertToShapes -- If true, convert bitmaps to twodshapes.
- matteColor -- if defined, set bitmap's matte to this color.
- invisibleColor -- if defined, set bitmaps's invisible color to this color.
- class methods
- method init self #rest args ->
- (
- apply nextMethod self args
- local em := new KeyedLinkedList
- add em ".dib" #(@Image, @DIB, @Bitmap)
- add em ".bmp" #(@Image, @DIB, @Bitmap)
- add em ".pic" #(@Image, @PICT, @Bitmap)
- add em ".aif" #(@Sound, @AIFF, @Player)
- add em ".wav" #(@Sound, @Wave, @Player)
- add em ".avi" #(@Movie, @AVI, @Player)
- add em ".mov" #(@Movie, @Quicktime, @Player)
- add em ".rtf" #(@Text, @RTF, @RichText)
- self.extensionMapping := em
- self
- )
- end
-
- -- Method init saves keyword arguments in instance variables.
- method init self {class MediaImporter} #rest args #key \
- path: (theStartDir) \
- container: (undefined) \
- target: (undefined) \
- addExtensions: (false) \
- smartExtensions: (false) \
- extension: ("") \
- mediaCategory: (@image) \
- inputMediaType: (@dib) \
- outputMediaType: (@bitmap) \
- convertToShapes: (true) \
- mode: (@create) ->
- (
- apply nextMethod self args
- self.path := path
- self.container := container
- self.target := target
- self.addExtensions := addExtensions
- self.smartExtensions := smartExtensions
- self.extension := extension
- self.mediaCategory := mediaCategory
- self.inputMediaType := inputMediaType
- self.outputMediaType := outputMediaType
- self.matteColor := undefined
- self.invisibleColor := undefined
- self.convertToShapes := convertToShapes
- return self
- )
-
- method getOne self {class MediaImporter} key ->
- (
- getOne self.target key
- )
-
- -- Method inferMediaType parses a filename to get its extension, and then
- -- calls setExtension on self.
- method inferMediaType self {class MediaImporter} fileName ->
- (
- -- Get extension part of filename.
- local ext := new String
- for i := ((size fileName) - 3) to (size fileName) do
- (
- append ext fileName[i]
- )
- setExtension self ext
- )
-
- method stripExtension self {class MediaImporter} fileName ->
- (
- -- Get prefix part of filename.
- local prefx := new String
- for i := 1 to ((size fileName) - 4) do
- (
- append prefx fileName[i]
- )
- return prefx
- )
-
- -- Method setExtension sets the current file extension to the specified
- -- value. It also automatically sets the import parameters based the
- -- MediaImporter.extensionMapping.
- method setExtension self {class MediaImporter} extension ->
- (
- self.extension := extension
- local result := MediaImporter.extensionMapping[extension]
- if (result <> empty) do
- (
- self.mediaCategory := result[1]
- self.inputMediaType := result[2]
- self.outputMediaType := result[3]
- )
- )
-
- -- Method processBitmap is called to process an imported bitmap, based on
- -- MediaImporter parameters.
- method processBitmap self {class MediaImporter} bm ->
- (
- if (self.invisibleColor <> undefined) do
- (
- bm.invisibleColor := self.invisibleColor
- )
- if (self.matteColor <> undefined) do
- (
- bm.matteColor := self.matteColor
- )
- if (self.convertToShapes) do
- (
- bm := new TwoDShape boundary:bm fill:blackBrush
- )
- return bm
- )
-
- -- Method storeMedia handles adding the media to the storage container
- -- and target keyed collection, if either has been specified.
- method storeMedia self {class MediaImporter} name media ->
- (
- -- proxy for media if using a storage container.
- local px
- if self.container <> undefined do
- (
- -- Register the media with the storage container.
- px := addToStorageContainer media self.container
- store px
- makePurgeable px
- )
- if self.target <> undefined do
- (
- -- Add the media to the explicit keyed collection.
- if self.container <> undefined then
- (
- -- Store the proxy if using a storage container.
- setOne self.target name px
- ) else
- (
- -- Store the media if not using a storage container.
- setOne self.target name media
- )
- )
- )
-
- method importObject self {class MediaImporter} fileName #key mediaKey: (undefined) \
- mediaStream:(undefined) ->
- (
- local mStream
- local mKey := fileName
- if (mediaStream <> undefined) then
- (
- mStream := mediaStream
- ) else
- (
- -- Create a media stream for the specified path, and convert to output media type.
- local fullName
- if (self.addExtensions) then
- (
- fullName := fileName + self.extension
- )
- else (
- fullName := fileName
- )
- if (self.smartExtensions) do
- (
- -- Infer media type, and strip extension from filename.
- inferMediaType self fullName
- mKey := stripExtension self fullName
- )
- -- Build path for Media folder and append fullName.
- local p := copy (self.path as Sequence)
- append p fullName
- mStream := getStream theRootDir p @Readable
- )
- local media := importMedia theImportExportEngine mStream self.mediaCategory self.inputMediaType self.outputMediaType \
- container:self.container colorMap: defaultColorMap
- -- If a mediaKey is specified, use it.
- if (mediaKey <> undefined) do mKey := mediaKey
-
- -- If it is a bitmap, call the processBitmap method.
- if (self.mediaCategory = @Image) do
- (
- media := processBitmap self media
- )
- -- Store the media.
- storeMedia self mKey media
- plug mStream
- return media
- )
-
- -- Method importAllMedia iterates through every file in the media importer's path,
- -- and calls importObject.
- method importAllMedia self {class MediaImporter} ->
- (
- -- Iterate through all files in the path.
- local failList := new Array
- local theErr := @all
- -- Iterate through all of the files.
- for fileName in (getContents self.path) do
- (
- -- If it is a file (not a directory)...
- if (isFile self.path fileName) do
- (
- guard
- (
- importObject self fileName
- )
- catching
- theErr : (
- append failList fileName
- caught theErr
- )
- on exit
- undefined
- end
- )
- )
- )
-
- -- The following methods support specific media types, and use the more
- -- general importObject method.
- method importPICT self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".pic"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importRTF self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".rtf"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importAIFF self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".aif"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importWave self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".wav"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importDIB self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".bmp"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importQuicktime self {class MediaImporter} fileName #key mediaKey: ->
- (
- setExtension self ".mov"
- return importObject self fileName mediaKey:mediaKey
- )
-
- method importRes self {class MediaImporter} type id #key mediaKey: (undefined) ->
- (
- setExtension self ".pic"
- -- If no media key is specified, use the res file id.
- local str := getOneStream self.bundle type:type ID:id
- return importObject self (id as String) mediaStream:str mediaKey:mediaKey
- )
-
- -- Register the target explicit keyed collection with the container, and save it.
- method saveMedia self {class MediaImporter} name ->
- (
- registerPersistentRoot self.target name self.container
- store self.target
- )
- -->>>
- "Compiled mediaimp.sx"
-